﻿#region Dont Look This

// /*************************************************************************************
//
// * 文件名称：  MyConfig.cs
//
// * 作       者：  jieaido@outlook.com
//
// * 创建日期：  2020/04/04/21:01
//
// * 修改日期:     2020/04/06/21:01
//
// * 功能描述：
//
//
//
//                               Copyright (c) Jieaido. AllRights Reserved
//
// *************************************************************************************/

#endregion Dont Look This

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using HelpLibrary.MyLog;

namespace HelpLibrary.MyConfig
{
    public class MyConfig
    {
        private readonly XmlDocument _doc;
        private readonly XmlNode _rootNode;
        private readonly string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MyConfig\\Tag.config");

        public MyConfig()
        {
            _doc = new XmlDocument();
            try
            {
                _doc.Load(filepath);
            }
            catch (Exception e)
            {
                MyLogHelper.LogHelper.WriteError(e.ToString());
                throw;
            }

            _rootNode = _doc.DocumentElement;
        }

        /// <summary>
        ///     弃用
        /// </summary>
        public void createxml()
        {
            var node1 = _doc.SelectSingleNode("config/tagnames");
            var node2 = _doc.SelectNodes("config/tagnames/*");
            var ss = _rootNode.SelectSingleNode("tagnames");
            // ss.RemoveAll();
            var newNode = _doc.CreateElement("random1");

            // newNode.InnerText = "fuckyou";
            node1.AppendChild(newNode);
            var xe = _doc.CreateElement("gogogo");
            xe.InnerText = "fuck";
            _rootNode.AppendChild(xe);
            _doc.Save(filepath);
            var nodelist = _doc.GetElementsByTagName("tagname");
            //XmlTextWriter tr = new XmlTextWriter(filepath, null);
            //tr.Formatting = Formatting.Indented;
            //_doc.WriteContentTo(tr);
            //tr.Close();
        }

        /// <summary>
        ///     弃用-从配置文件得到一个不重复的标签名称的列表
        /// </summary>
        /// <returns>返回一个标签名称的列表</returns>
        public HashSet<string> GetTagListFromConfig()
        {
            var itemTag = new HashSet<string>();
            if (_doc != null)
            {
                var node2 = _doc.SelectNodes("config/tagnames/*");
                foreach (XmlNode o in node2) itemTag.Add(o.LocalName);
            }

            return itemTag;
        }

        /// <summary>
        ///     弃用-添加标签名到配置文件
        /// </summary>
        /// <param name="itemTag">标签名</param>
        public void AddItemToConfig(string itemTag)
        {
            var tagnames = _rootNode.SelectSingleNode("tagnames");
            var tagnode = _doc.CreateElement(itemTag);
            if (tagnames != null) tagnames.AppendChild(tagnode);
            _doc.Save(filepath);
        }

        /// <summary>
        ///     弃用-
        /// </summary>
        /// <param name="itemTag"></param>
        public void DeleteItemFromConfig(string itemTag)
        {
            var tagnames = _rootNode.SelectSingleNode("tagnames");
            var tagname = _rootNode.SelectSingleNode("//" + itemTag);
            if (tagnames != null && tagname != null) tagnames.RemoveChild(tagname);
            _doc.Save(filepath);
        }

        /// <summary>
        ///     添加标签名,服务器名用于存储,最重要的方法
        /// </summary>
        /// <param name="itemTag">标签名</param>
        /// <param name="serverName">服务器名</param>
        /// <returns></returns>
        public string UpdateTag(string itemTag, string serverName)
        {
            var tagnode = _rootNode.SelectSingleNode("tagnames");
            var servernode = _rootNode.SelectSingleNode("servers");
            tagnode.InnerText = itemTag;
            servernode.InnerText = serverName;
            _doc.Save(filepath);
            return tagnode.InnerText;
        }

        /// <summary>
        ///     获取当前配置的标签名
        /// </summary>
        /// <returns></returns>
        public string GetTag()
        {
            return _rootNode.SelectSingleNode("tagnames")?.InnerText;
        }

        /// <summary>
        ///     获取当前配置的服务器名
        /// </summary>
        /// <returns></returns>
        public string GetServerName()
        {
            return _rootNode.SelectSingleNode("servers")?.InnerText;
        }

        /// <summary>
        ///     获取当前配置的班次信息
        /// </summary>
        /// <param name="workshift">班次</param>
        /// <returns></returns>
        public string GetBanci(out string workshift)
        {
            workshift = _rootNode.SelectSingleNode("banci/ban")?.InnerText;
            return _rootNode.SelectSingleNode("banci/time")?.InnerText;
        }

        /// <summary>
        ///     设置班次信息到配置
        /// </summary>
        /// <param name="dt">时间</param>
        /// <param name="Workshift">班次</param>
        public void SetBanci(DateTime dt, int Workshift)
        {
            _rootNode.SelectSingleNode("banci/time").InnerText = dt.ToShortDateString();
            _rootNode.SelectSingleNode("banci/ban").InnerText = Workshift.ToString();
            _doc.Save(filepath);
        }
    }
}